package com.ddy.test;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class DatePrint {
	private static String FORMAT = "yyyy-MM-dd HH:mm:ss";

	public static void main(String[] args) {
		try {
			testTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	public static void maina(String[] args) {
		String startTimeString = "2017-12-27 16:25:00";
		String endTimeString = "2017-12-27 17:10:00";
		try {
			Date startTime = convertStringToDate(startTimeString, FORMAT);
			Date endTime = convertStringToDate(endTimeString,FORMAT);
			System.out.println(startTime.getTime() + "---" + endTime.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	public static void testTime() throws ParseException {
		String beginTimeString = "2017-11-29 14:00:00";
		Date date = convertStringToDate(beginTimeString, "yyyy-MM-dd HH:mm:ss");
		System.out.println(date);
		System.out.println(new Date(1513239843711L));
		System.out.println(new Date(1513239843714L));

		System.out.println("System.currentTimeMillis()=" + System.currentTimeMillis());
		System.out.println("new Date().getTime()=" + new Date().getTime());

		Date start = convertStringToDate("2017-12-26 16:00:00","yyyy-MM-dd HH:mm:ss");
		Date end = convertStringToDate("2017-12-26 16:40:00","yyyy-MM-dd HH:mm:ss");
		long triggrTime=start.getTime();
		long middleTime = (end.getTime() + start.getTime())/2;
		System.out.println(convertDateToString(new Date(middleTime),FORMAT));
		while((triggrTime - middleTime) <= 0){
			triggrTime += 4*60*1000;
			if(triggrTime > middleTime){
				triggrTime = end.getTime() + 4*60*1000;
			}
			System.out.println("==> "+convertDateToString(new Date(triggrTime),FORMAT));
		}


	}


	public static Date convertStringToDate(String date, String format) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.parse(date);
	}
	public static String convertDateToString(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	public void test1(){
		Date date = new Date();
		long currentTimeMillis = System.currentTimeMillis();
		long tomorrowTimeMillis = currentTimeMillis + 1000*60*60*24;
		System.out.println(date.getTime());
		System.out.println(date.getTime()/1000);
		System.out.println(System.currentTimeMillis());
		System.out.println(getLocalTime("2016-07-14T02:11:34Z"));
		System.out.println(new Date());
		System.out.println(new Date(tomorrowTimeMillis));
		System.out.println(new Date(1503369556797L));
		System.out.println(new Date(1504134060000L));
		System.out.println("clientTime is " + new Date(1503391624731L));
		System.out.println("serverTime is " + new Date(1504074265813L));
		System.out.println("clientTime is " + new Date(1503545881582L));
		System.out.println("serverTime is " + new Date(1504074275816L));



	}

	
	public static void test(){
		DateFormat df = new SimpleDateFormat("dd\\/MMM\\/yyyy",Locale.ENGLISH);
        System.out.println(df.format(new Date()));
        
        System.out.println(TimeZone.getDefault().getID());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        try {
			System.out.println(sdf.parse("2016-07-14T02:11:34Z").toString());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static String getLocalTime(String utcTime){
		System.out.println(new Date().getTime() + "");
        String time = null;
        SimpleDateFormat datedf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        datedf.setTimeZone(TimeZone.getTimeZone("UTC"));
        SimpleDateFormat stringdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = datedf.parse(utcTime);
            time = stringdf.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }
}
